package com.Biomedica.virtualpowerpoint;

import java.io.IOException;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Set;

//import com.Biomedica.virtualpowerpoint.Laserpointer.LaserTrackingCallback;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.ViewSwitcher;

public class MainActivity extends Activity implements OnClickListener, com.Biomedica.virtualpowerpoint.Reciver.PacketRecived, OnTouchListener {

	
	
	private static final String version="L";
	
	
	
	Button btnnetx,btnprev,btnlast,btnfirst;
	Button btnpointer;
	Button btndraw,btnplay,btnblankscreen;
	
	
	
	//ClickerControll c=new ClickerControll();
	Sender _sender;
	Reciver _res;
	//Laserpointer _p;
	String IpAddress;
	/// Splash View
	ViewSwitcher vs;
	ProgressBar prog;
	TextView _splashtxt;
	
	Handler h=new Handler();

	// blue tooth
	BluetoothChannelSender _bt_sender;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
        	  this.InitaliseUI();
        	
      		ShowVresionUI();
      		 this.ShowWaitingBar("Connecting........");
           
        	ShowSplashScreen();
			_sender=new Sender();
			_res=new Reciver();
			_bt_sender=new BluetoothChannelSender(this.getApplicationContext());
			if(this.isWifiUsed())
			{
				// Wifi init signals
			_sender.Connect("255.255.255.255", 4445);
			_res.Start(4444);
			_res.setOnPacketRecived(this);
			_sender.Send("0x01");
			}
			else
			{
				// Blue tooth init signlas
				Set<BluetoothDevice> devices=_bt_sender.getpairedDevices();
			}
			
			
			
			
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        catch(Exception e)
        {
        	e.printStackTrace();
        }
      
       
      
    }

    private void ShowSplashScreen() {
		// TODO Auto-generated method stub
    	Intent SplashScreenInten=new Intent(this,SplashScreen.class);
    	this.startActivity(SplashScreenInten);
	}

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		switch(item.getItemId())
		{
		case R.id.menu_settings:// Display the Setting  Activity
			Intent control_intent=new Intent(this,SettingsActivity.class);
			this.startActivity(control_intent);
			break;
		case R.id.mnhelp:// Display the Help Activity
			Intent help_intent=new Intent(this,HelpActivity.class);
			this.startActivity(help_intent);
			break;
		case R.id.switchsource:// Switch between BT-WIFI
			if(isWifiUsed()) UseWifiAsSource(true);
			else
				UseWifiAsSource(false);
			break;
		
		}
		return true;
		
	}
    /*
     * Check if the Current Oprion of transmition is wifi
     */
	private boolean isWifiUsed() {
		// TODO Auto-generated method stub
		return true;
	}
	/*
	 * 
	 * toggle between the wifi or BT
	 */

	private void UseWifiAsSource(boolean b) {
		// TODO Auto-generated method stub
		
		
	}

	@Override
    protected void onStart ()
    {
    	super.onStart();
    	//_p.LaserStartTracking();
    	
    }
    @Override
    protected void onPause ()
    {
    	super.onPause();
    	//_p.LaserStopTracking();
    	
    }
	

	public void OnPacketRecived(String Msg) {
		// TODO Auto-generated method stub
		
		if(Msg.equals((String)Msg))
		{
			//HideSplashScreen();
			if(Msg.equals("0x21"))
			{
				ShowPingBox();
			}
			
		}
		
		
		
	}

	private void ShowPingBox() {
		// TODO Auto-generated method stub
		 AlertDialog.Builder builder = new AlertDialog.Builder(this.getApplicationContext());
		
		   builder.setCancelable(true);
		
		  // builder.setIcon(Icon);
		
		   builder.setTitle("Chaneel Verification Passed .......... ");
		
		   builder.setInverseBackgroundForced(true);
		
		  builder.show();

	}

	private void HideSplashScreen() {
		// TODO Auto-generated method stub
		vs.getNextView();
		
	}

	/////////////////////////////////////////////////// internal functions  /////////////////////////////////////
	// initalise the UI Elements
	private void InitaliseUI()
	{
		
		ShowScreen=false;
		
		btnnetx=(Button)findViewById(R.id.btnnext);
		btnprev=(Button)findViewById(R.id.btnprev);
		btnfirst=(Button)findViewById(R.id.btnfirst);
		btnlast=(Button)findViewById(R.id.btnlast);
		btnpointer=(Button)findViewById(R.id.pointer);
		
		
		btnpointer.setOnTouchListener(this);
		
		btnnetx.setOnClickListener(this);
		btnprev.setOnClickListener(this);
		btnfirst.setOnClickListener(this);
		btnlast.setOnClickListener(this);
	    btnpointer.setOnClickListener(this); 
		
	
		
		//_p=new Laserpointer(this.getApplicationContext());
		//_p.setLaserTrackingCallback(this);
		
		
		
		
		
	}
	Intent intent;
	// Assigne the layout and make it bublic



	private boolean ShowScreen;
	
	
	
	public void ShowProgressDlgBox(android.content.Context context,String Message)
	{
		ProgressDialog dialog = ProgressDialog.show(context, "", 
                Message, true);
		dialog.show();
		
	}
	public void ShowTouchScreen()
	{
		Intent touch_intent=new Intent(this,TochscreenActivity.class);
		this.startActivity(touch_intent);
	}
	
	
	
/*
	@Override
	public void OnLaserMoveOccure(String Code) {
		// TODO Auto-generated method stub
		try {
			_sender.Send(Code);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	*/
	 private void ShowWaitingBar(String txt)
		{
			
			try {
				prog.setVisibility(ProgressBar.VISIBLE);
				_splashtxt.setText(txt);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}

	    private void HideWaitingBar(String txt)
		{
			prog.setVisibility(View.INVISIBLE);
		}

		public void onClick(View v) {
			// TODO Auto-generated method stub
			String code=(String)v.getTag();
			if(code.equals("0x10"))
			{
				if(this.isShowScreen())
					try {
						_sender.Send("0x1e");
						setShowScreen(false);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				else
					try {
						_sender.Send("0x1f");
						setShowScreen(true);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			}
			else
			{
				try {
					_sender.Send(code);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

}

		private void setShowScreen(boolean b) {
			// TODO Auto-generated method stub
			ShowScreen=b;
			
		}

		private boolean isShowScreen() {
			// TODO Auto-generated method stub
			return ShowScreen;
		}

		/*
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
			switch(v.getId())
			{
			case(R.id.pointer):
				if(event.getAction()==MotionEvent.ACTION_DOWN)
				{
					SendStartPointer(true);
				}
				else if(event.getAction()==MotionEvent.ACTION_UP)
				{
					SendStartPointer(false);
				}
				break;
			
			
			
			}
			return true;
		}
*/
		private void SendStartPointer(boolean b) {
			// TODO Auto-generated method stub
			if(b) // Send Show pointer Message
				try {
					_sender.Send("0x10");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
			else // Hide pointer Message
				try {
					_sender.Send("0x16");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			
		}
	    
		private void ShowVresionUI()
		{
			if(version.equals((String)"S"))
			{
				// enable the Standered version features
			}
			else if(version.equals((String)"F"))
			{
			    // enable the Full features
			}
			else if(version.equals((String)"L"))
			{
				// Show and Hide Lite features
			}
				
		}
		public void SendCode(String code) throws IOException
		{
			if(this.isWifiUsed()) _sender.Send(code);
			else _bt_sender.Send(code);
		}

		public boolean onTouch(View arg0, MotionEvent arg1) {
			// TODO Auto-generated method stub
			return false;
		}

		
}
